package com.kb.design.mapper.postgreSQL;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kb.design.dto.DesignProjectDTO;
import com.kb.design.entity.CustomUser;
import com.kb.design.entity.DesignProject;
import com.kb.design.vo.ProjectListVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 附件表 Mapper 接口
 * </p>
 *
 * @author author
 * @since 2023-11-14
 */
@Mapper
public interface ProjectMapper extends BaseMapper<DesignProject> {
    Page<ProjectListVO> queryByPage(Page<DesignProjectDTO> page , DesignProjectDTO projectDTO);
    Page<ProjectListVO> queryProjectByDesignLeader(Page<DesignProjectDTO> page , DesignProjectDTO projectDTO);


    Page<ProjectListVO> queryBonusByPage(Page<DesignProjectDTO> page , DesignProjectDTO projectDTO);
    Page<ProjectListVO> queryBonusAdminByPage(Page<DesignProjectDTO> page , DesignProjectDTO projectDTO);

//    @Select("SELECT r.* FROM design_project p LEFT JOIN design_responsible_person r ON p.id = r.project_id WHERE r.del = '0' and p.id = #{id}")
//    public List<DesignResponsiblePerson> searchDetailsInfo(Long id);

    @Select({
            "<script>",
            "SELECT T.* FROM design_user u",
            "LEFT JOIN design_user T ON u.dept_id = T.dept_id",
            "LEFT JOIN design_user_role ur ON ur.user_id = T.id",
            "LEFT JOIN design_role r ON r.role_id = ur.role_id",
            "WHERE u.del_flag = '0'",
            "AND T.del_flag = '0'",
            "AND r.role_key LIKE '%Competent'",
            "AND u.code = #{code}",
            "UNION",
            "SELECT u.* FROM design_user u",
            "WHERE u.del_flag = '0'",
            "AND u.code = #{code}",
            "</script>"
    })
    public List<CustomUser> queryUserListByCode(String code);

    /**
     * 根据用户code 查询关联项目（数组匹配）
     */
    @Select("SELECT * FROM design_project p WHERE p.relevant_responsible_person && ARRAY[${code}]::integer[] and life_cycle = TRUE")
    List<DesignProject> queryProjectListByCode(@Param("code") Integer code);

    /**
     * 根据参数查询项目
     */
    @Select("SELECT * FROM design_project WHERE (array_length(relevant_responsible_person, 1) IS NOT NULL) AND (relevant_responsible_person[array_length(relevant_responsible_person, 1)] = #{code}) and life_cycle = TRUE")
    List<DesignProject> queryProjectListByPerson(@Param("code") Integer code);

    List<DesignProject> getProjectTypeCount(DesignProjectDTO projectDTO);

    void updateProjectInfo(DesignProject project);
    void replacementUpdate(DesignProject project);


}
